<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="padding: 10px 0">
              <el-button
                v-if="canClick('p-ro-add')"
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="showDialog = true"
                >新增角色
              </el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template v-slot="obj">
                  <el-button
                    size="small"
                    type="success"
                    @click="openAssignPerm(obj.row.id)"
                  >
                    分配权限
                  </el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="editRole(obj.row.id)"
                    >编辑
                  </el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="delRole(obj.row)"
                    >删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                :current-page="query.page"
                :page-size="query.pagesize"
                :total="query.total"
                layout="prev,pager,next"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 新增角色弹框 -->
    <template>
      <!-- 新增弹框 -->
      <el-dialog title="编辑弹层" :visible="showDialog" @close="closeDig">
        <el-form
          ref="roleForm"
          :model="roleForm"
          :rules="rules"
          label-width="100px"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="closeDig">取消</el-button>
            <el-button size="small" type="primary" @click="btnOK">
              确定
            </el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </template>
    <!-- 给角色分配权限 -->
    <AssignPerm
      ref="AssignPermRef"
      :show-assign-dialog.sync="showAssignDialog"
    ></AssignPerm>
  </div>
</template>

<script>
import { getRoleList, deleteRole, addRole, getRoleDetail, updateRole } from '@/api/setting.js'

import AssignPerm from './conponents/AssignPerm'
export default {
  components: {
    AssignPerm
  },
  data () {
    return {
      //
      showAssignDialog: false,
      // 角色列表
      list: [],
      query: {
        page: 1,
        pagesize: 10,
        total: 0
      },
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 打开给角色分配权限弹层
    openAssignPerm (id) {
      console.log(id)
      this.showAssignDialog = true
      // 调用子组件 getRolePerms 获取回显数据
      this.$refs.AssignPermRef.getRolePerms(id)
    },
    async getList () {
      const { rows, total } = await getRoleList(this.query)
      this.list = rows
      this.query.total = total
    },
    changePage (newPage) {
      this.query.page = newPage
      console.log(newPage)
      this.getList()
    },
    // 删除当前角色
    async delRole (row) {
      console.log(row)
      // 1.用户确认
      // 2.确认之后，调用接口删除
      // 3.刷新列表
      // 4.提示消息
      try {
        await this.$confirm('确认删除' + row.name + '这个角色嘛')
        await deleteRole(row.id)
        this.getList()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 关闭弹框
    closeDig () {
      // 清空表单数据
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
      this.showDialog = false
    },
    // 提交
    async btnOK () {
      // 1.表单校验
      // 2.调用接口
      // 3.提示信息 刷新列表
      try {
        await this.$refs.roleForm.validate()
        // console.log('cg')
        if (this.roleForm.id) {
          await updateRole(this.roleForm)
          this.$message.success('更新成功')
        } else {
          await addRole(this.roleForm)
          this.$message.success('新增成功')
        }
        this.closeDig()
        this.getList()
      } catch (error) {
        this.$message.error('新增失败')
        return error
      }
    },
    // 编辑
    async editRole (id) {
      console.log(id)
      // 根据id 获取当前数据
      const obj = await getRoleDetail(id)
      this.roleForm = obj
      this.showDialog = true
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
